<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <canvas id="canvas"></canvas>
    <script>
      const canvas = document.getElementById("canvas");
      const ctx = canvas.getContext("2d");

      canvas.width = window.innerWidth;
      canvas.height = window.innerHeight;

      ctx.translate(canvas.width / 2, canvas.height);
      ctx.scale(1, -1);

      function drawBranch(start, thick, angle, length) {
        if (length < 10) {
          ctx.beginPath();
          ctx.arc(...start, 2, 0, 2 * Math.PI);
          ctx.fillStyle = "#b381b3";
          ctx.fill();
          return;
        }
        if (thick < 2 && Math.random() < 0.1) return;
        ctx.beginPath();
        ctx.moveTo(...start);
        const end = [
          start[0] + length * Math.cos((angle * Math.PI) / 180),
          start[1] + length * Math.sin((angle * Math.PI) / 180),
        ];
        ctx.lineTo(...end);
        ctx.strokestyle = "#333";
        ctx.lineWidth = thick;
        ctx.stroke();

        // 绘制左分支
        drawBranch(end, thick * 0.8, angle - Math.random() * 20, length * 0.8);
        // 绘制右分支
        drawBranch(end, thick * 0.8, angle + Math.random() * 20, length * 0.8);
      }

      drawBranch([0, 0], 6, 90, 100);
    </script>
  </body>
</html>
